/* eslint-disable import/consistent-type-specifier-style */
import { Injectable } from "@angular/core";
import type { Hero } from "../interface/hero";
import { TextFormatter } from "../utils/textFormatter";

// This Angular service will be "downgraded" to be used in AngularJS
@Injectable()
export class HeroesService {
  heroes: Hero[] = [
    { name: "superman", description: "The man of steel" },
    { name: "wonder woman", description: "Princess of the Amazons" },
    { name: "thor", description: "The hammer-wielding god" },
  ];

  constructor(textFormatter: TextFormatter) {
    // Change all the hero names to title case, using the "upgraded" AngularJS service
    // eslint-disable-next-line unicorn/no-array-for-each
    this.heroes.forEach(
      (hero: Hero) => (hero.name = textFormatter.titleCase(hero.name)),
    );
  }

  addHero() {
    this.heroes = [
      ...this.heroes,
      { name: "Kamala Khan", description: "Epic shape-shifting healer" },
    ];
  }

  removeHero(hero: Hero) {
    this.heroes = this.heroes.filter((item: Hero) => item !== hero);
  }
}
